﻿var ns = namespace("kaskelot.sp.ui");

ns.kurstillfallesearch = function () {
	var cookieName = "kurstillfallesearch";

	function init() {
		var c = kaskelot.sp.ui.cookies.getCookie(cookieName);
		if (c != undefined) {
			$("#KursDropDownList").val(c.kursId).trigger("liszt:updated");
			getList();
		}

		$("#title-container").html("Kurstillfällen");
		$("#tab-container ul").hide();

		$(".chzn-select-deselect").chosen({ allow_single_deselect: true, disable_search: true });

		$("#search-button").click(function () {
			getList();
		});

		$("#clear-button").click(function () {
			$("#KursDropDownList").val("").trigger("liszt:updated");
		});
	}

	function getList() {
		var id = $("#KursDropDownList").val();

		var data = { kursId: id };
		kaskelot.sp.ui.cookies.setCookie(cookieName, data);

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/GetKursTillfallenByKursId",
			type: "GET",
			data: data,
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetKursTillfallenByKursIdResult.HasError) {
						$("#kurstillfalle-search-results tbody").html(
							$("#search-kurstillfalle-template").render(data.GetKursTillfallenByKursIdResult.Data)
						);
					}
					$(".blockable-container").unblock();
				},
			error:
				function (xhr) {
					$(".blockable-container").unblock();
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function createKurstillfalle() {
		var id = getParameterByName("KurstillfalleId");

		var selector = "#kurstillfalle-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "KursTillfalleForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$("#kurstillfalle-form").dialog({ title: "Skapa kurstillfälle" });
				$(".chzn-select-deselect").chosen({ allow_single_deselect: true, disable_search: true });

				$("#KursDatum").datepicker();;
				$("#Starttid").datetimepicker({ showButtonPanel: false, stepMinute: 5, timeOnly: true });
				$("#Sluttid").datetimepicker({ showButtonPanel: false, stepMinute: 5, timeOnly: true });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "CreateKursTillfalle";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	return {
		init: init,
		getList: getList,
		createKurstillfalle: createKurstillfalle
	};
}();